package com.lufei.dh.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.lufei.dh.List.Adapter.Adapter_List;
import com.lufei.dh.R;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/3/26 0026.
 */

public class Main_List extends Activity {
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    private Adapter_List adapterList;
    private List<String> data = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_activity);
        ButterKnife.bind(this);
        setAdapterList();

        getBannerData();
    }

    public void setAdapterList() {
        adapterList = new Adapter_List(data, this);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerview.setLayoutManager(layoutManager);
        recyclerview.setAdapter(adapterList);

    }
    private void getBannerData() {

        OkGo.<String>get("http://172.16.4.167:8080/image/list")
                .execute(new StringCallback() {

                    @Override
                    public void onSuccess(Response<String> response) {

                        List<String> objects = GsonUtils.GsonToList(response.body(), null);
                        Log.e("OkGo网络数据", response.body() + "\n" +objects);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        Log.e("OkGo", "ccccccccc"+response.message());
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        try {
                            JSONArray jsonArray=new JSONArray(response.body());
                            for (int i=0;i<jsonArray.length();i++){
                                data.add(jsonArray.get(i).toString());
                            }
                        } catch (JSONException e) {


                        }
                        Log.e("OkGo读取缓存",response.body() + "\n"+data );
                    }

                });
    }
}
